package com.sinodata.bsm.cicp.collector.jboss;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.RmiUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: Jboss连接池各项指标的采集
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-24 下午09:52:09          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ManagedConnectionPoolCollector extends Collector {
    private static Logger logger = Logger.getLogger(ManagedConnectionPoolCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        //        String getManagerName = this.getParamMap().get("名称").trim();
        String getManagerName = this.getParamMap().get("Name").trim();
        String managedConnectionPoolName = null;
        try {
            //            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("端口")));
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("Port")));
            Set<ObjectName> objectNames = RmiUtil.queryObjectNames(rmiAdapter, "jboss.jca:service=ManagedConnectionPool,*");
            for (ObjectName jbossManagedConnectionPool : objectNames) {
                managedConnectionPoolName = (String) rmiAdapter.getAttribute(jbossManagedConnectionPool, "PoolJndiName");
                if (getManagerName.equals(managedConnectionPoolName)) {
                    Integer maxSizeValue = (Integer) rmiAdapter.getAttribute(jbossManagedConnectionPool, "MaxSize");
                    Long inUseConnectionCountValue = (Long) rmiAdapter.getAttribute(jbossManagedConnectionPool, "InUseConnectionCount");
                    String state = rmiAdapter.getAttribute(jbossManagedConnectionPool, "State") + "";
                    String maxSize = maxSizeValue + "";
                    String maxConnectionsInUseCount = rmiAdapter.getAttribute(jbossManagedConnectionPool, "MaxConnectionsInUseCount") + "";
                    String connectionCount = rmiAdapter.getAttribute(jbossManagedConnectionPool, "ConnectionCount") + "";
                    String availableConnectionCount = rmiAdapter.getAttribute(jbossManagedConnectionPool, "AvailableConnectionCount") + "";
                    String connectionCreatedCount = rmiAdapter.getAttribute(jbossManagedConnectionPool, "ConnectionCreatedCount") + "";
                    String inUseConnectionCount = inUseConnectionCountValue + "";
                    String inUseRatio = inUseConnectionCountValue * 100 / maxSizeValue + "";

                    propValues[0] = ResPropertyValue.createPropValue(state, propMetas[0]);
                    propValues[1] = ResPropertyValue.createPropValue(maxSize, propMetas[1]);
                    propValues[2] = ResPropertyValue.createPropValue(maxConnectionsInUseCount, propMetas[2]);
                    propValues[3] = ResPropertyValue.createPropValue(connectionCount, propMetas[3]);
                    propValues[4] = ResPropertyValue.createPropValue(availableConnectionCount, propMetas[4]);
                    propValues[5] = ResPropertyValue.createPropValue(connectionCreatedCount, propMetas[5]);
                    propValues[6] = ResPropertyValue.createPropValue(inUseConnectionCount, propMetas[6]);
                    propValues[7] = ResPropertyValue.createPropValue(inUseRatio, propMetas[7]);

                    propertyValueX.setValues(propValues);
                    values.add(propertyValueX);
                    return values;
                }
            }
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }

        return null;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口", "名称" };
        return new String[] { "Port", "Name" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "状态", "最大大小", "最大使用连接数", "连接次数", "可用连接数", "创建连接数", "使用数量", "使用率" };
        //        return new String[] { "Status", "MaxSize", "MacConnsNum", "ConnsNum", "AvailableConnsNum", "CreateConnsNum", "UsingNum", "Usage" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8" };
    }

}
